package com.cainli.gcmdemo;

import java.io.IOException;
import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;


public class GCMIntentService extends GCMBaseIntentService {
	private static final String TAG = GCMIntentService.class.getSimpleName();

	public interface MessageListener{
		public void onRecvMessage(String msg);
		public void onRegistered(String registrationId);
	}
	
	public static MessageListener listener;

	public static void setListener(MessageListener listener) {
		GCMIntentService.listener = listener;
	}

	@Override
	protected void onError(Context context, String err) {
		Toast.makeText(context, err, Toast.LENGTH_SHORT).show();
		Log.v(TAG,"Error:"+err);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		Log.v(TAG, "C2DMReceiver Message");
		Bundle extras = intent.getExtras();
		if(extras!=null){
			//根据键值，提取对应的消息内容
			Object oMsg = extras.get("msg");
			String msg = oMsg==null ?"":(String)oMsg;
			Object oID = extras.get("id");
			String id = oID==null ?"":(String)oID;
			Object oTime = extras.get("time");
			String time = oTime==null ?"":(String)oTime;
			StringBuffer sb = new StringBuffer();
			sb.append(id).append("   ")
			.append(time).append("   ")
			.append(Calendar.getInstance().getTime().getTime()).append("   ")
			.append(msg).append("   ")
			.append("\n");
			try {
				Utils.saveMsg2(sb.toString());
			} catch (IOException e) {
				Log.e(TAG,e.getMessage());
			}
//			Utils.saveMsg(sb.toString());
			Log.v(TAG, "The received msg = "+sb.toString());
			if(listener!=null){
				listener.onRecvMessage(sb.toString());
			}
//			//在标题栏上显示通知
//			NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//			Notification notification = new Notification(R.drawable.ic_launcher, msg, System.currentTimeMillis());
//			PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
//			notification.setLatestEventInfo(context, context.getString(R.string.app_name), msg, contentIntent);
//			notificationManager.notify(0, notification);			
		}
	}

	
	

	@Override
	protected void onRegistered(Context context, String registrationId) {
		Log.v(TAG,registrationId);
		final SharedPreferences prefs = context.getSharedPreferences(Constants.GCM_PREFERENCE,Context.MODE_PRIVATE);
        Editor editor = prefs.edit();
        editor.putString(Constants.GCM_REGISTRATION, registrationId);
        editor.commit();
		//创建数据文件
		if(!Utils.createDataFile(Constants.ID_FILE_PATH)){
			Utils.saveID(registrationId);
			Toast.makeText(this, "ID不能存储文件！", Toast.LENGTH_SHORT).show();
		}
        if(listener!=null){
			listener.onRegistered(registrationId);
		}
	}

	@Override
	protected void onUnregistered(Context context, String arg1) {
		Log.v(TAG,arg1);
		  final SharedPreferences prefs = context.getSharedPreferences(Constants.GCM_PREFERENCE,Context.MODE_PRIVATE);
	        Editor editor = prefs.edit();
	        editor.putString(Constants.GCM_REGISTRATION, "");
	        editor.commit();
	}
}
